package kr.saeildamil.androidStudy_second.brservice;

import kr.saeildamil.androidStudy_second.R;
import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class NewsService extends IntentService {
	boolean mQuit;

	public NewsService() {
		super("NewsService");
	}

	@Override
	protected void onHandleIntent(Intent arg0) {
		Message msg;
		Log.d("NewsIntentService", "onHandleIntent()");
		String[] arNews = getResources().getStringArray(R.array.PLANET);
		for(int idx = 0; mQuit== false; idx++){
			msg = new Message();
			msg.what=0;
			msg.obj = arNews[idx % arNews.length];
			mHandler.sendMessage(msg);
			try{
				Thread.sleep(5000);
			} catch (Exception e){}
		}
	}
	
	Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what == 0){
				Toast.makeText(NewsService.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
			}
		}
		
	};

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d("NewsIntentService", "onStartCommand()");
		mQuit = false;
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		Log.d("NewsIntentService", "onDestroy()");
		super.onDestroy();
		mQuit = true;
	}
	
	
	

}
